package com.lx.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lx.model.*;
import com.lx.service.MessageService;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
@Component
public class MessageReceiver {

    @Autowired
    private MessageService messageService;

    private static String CHAR_ENCODING = "UTF-8";

//    protected Log logger = LogFactory.getLog(this.getClass());
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * MQ消息监听：菜品分类消息处理
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${mq.queue.dishtype}", durable = "true", exclusive = "false", autoDelete = "true"),
            exchange = @Exchange(value = "${mq.dish.exchange}", durable = "true", autoDelete = "false", type = ExchangeTypes.TOPIC), key = "${mq.queue.dishtype.tk}"
    ))
    public void handleDishTypeMsg(String msg) {
        logger.info("----dishType msg-----"+msg);
        PrintDishType printDishType = JSONObject.parseObject(msg, PrintDishType.class);
        messageService.saveDishType(printDishType);

    }


    /**
     * MQ消息监听：门店菜品消息处理
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${mq.queue.shopdish}", durable = "true", exclusive = "false", autoDelete = "true"),
            exchange = @Exchange(value = "${mq.dish.exchange}", durable = "true", autoDelete = "false", type = ExchangeTypes.TOPIC), key = "${mq.queue.shopdish.tk}"
    ))
    public void handleShopDish(String msg) {
        logger.info("----dish msg-----"+msg);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(msg);

        Long shopDishId = jsonObject.getLong("shopDishId");

        PrintShopDish printShopDish = JSONObject.toJavaObject(jsonObject, PrintShopDish.class);

        printShopDish.setId(shopDishId);

        messageService.savePrintShopDish(printShopDish);

    }


    /**
     * MQ消息监听：餐桌信息处理
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${mq.queue.tableinfo}", durable = "true", exclusive = "false", autoDelete = "true"),
            exchange = @Exchange(value = "${mq.table.exchange}", durable = "true", autoDelete = "false", type = ExchangeTypes.TOPIC), key = "${mq.queue.tableinfo.tk}"
    ))
    public void handleTableInfo(String msg) {
        logger.info("----tableInfo msg-----"+msg);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(msg);

        String tableTypeName = jsonObject.getString("typeName");
        String tableAreaName = jsonObject.getString("areaName");

        PrintTableInfo printTableInfo = JSONObject.toJavaObject(jsonObject, PrintTableInfo.class);

        printTableInfo.setTableTypeName(tableTypeName);
        printTableInfo.setTableAreaName(tableAreaName);

        messageService.savePrintTableInfo(printTableInfo);

    }


    /**
     * MQ消息监听：餐桌类型消息处理
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${mq.queue.tabletype}", durable = "true", exclusive = "false", autoDelete = "true"),
            exchange = @Exchange(value = "${mq.table.exchange}", durable = "true", autoDelete = "false", type = ExchangeTypes.TOPIC), key = "${mq.queue.tabletype.tk}"
    ))
    public void handleTableType(String msg) {
        logger.info("----tableType msg-----"+msg);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(msg);

        String tableTypeName = jsonObject.getString("typeName");

        PrintTableType printTableType = JSONObject.toJavaObject(jsonObject, PrintTableType.class);

        printTableType.setTableTypeName(tableTypeName);
        messageService.savePrintTableType(printTableType);
    }

    /**
     * MQ消息监听：餐桌区域消息处理
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${mq.queue.tablearea}", durable = "true", exclusive = "false", autoDelete = "true"),
            exchange = @Exchange(value = "${mq.table.exchange}", durable = "true", autoDelete = "false", type = ExchangeTypes.TOPIC), key = "${mq.queue.tablearea.tk}"
    ))
    public void handleTableArea(String msg) {
        logger.info("----area msg-----"+msg);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(msg);
        String areaName = jsonObject.getString("areaName");

        PrintTableArea printTableArea = JSONObject.toJavaObject(jsonObject, PrintTableArea.class);

        printTableArea.setTableAreaName(areaName);

        messageService.savePrintTableArea(printTableArea);
    }

    /**
     * MQ消息监听：订单消息处理
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${mq.queue.orderinfo}", durable = "true", exclusive = "false", autoDelete = "true"),
            exchange = @Exchange(value = "${mq.orderinf.exchange}", durable = "true", autoDelete = "false", type = ExchangeTypes.TOPIC), key = "${mq.queue.orderinfo.tk}"
    ))
    public void handleOrder(String msg) {
        logger.info("----order msg-----"+msg);

        PrintOrderInfo info = JSONObject.parseObject(msg,PrintOrderInfo.class);

        messageService.savePrintOrderInfo(info);
    }


}
